package com.xjc.business.controller.colladmin;

import com.xjc.business.entity.po.coladmin.MajorPo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.coladmin.MajorVo;
import com.xjc.business.entity.vo.coladmin.MajorWithClass;
import com.xjc.business.service.MajorsService;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.exception.MyException;
import com.xjc.system.result.Result;
import com.xjc.system.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 徐锦程
 * 时间:2025-01-29 11:55
 */
@RestController("col_major_controller")
@RequestMapping("/college_admin/major")
public class MajorController {

    @Autowired
    MajorsService majorsService;

    /**
     * 获取专业分页
     * @param pageSize 每页条数
     * @param currentPage 当前页
     * @param collegeId 学院id
     * @param majorName 专业名称
     * @return 数据
     */
    @GetMapping("/page/{pageSize}/{currentPage}")
    public Result<PageVo<MajorVo>> getMajorPage(
            @PathVariable(value = "pageSize",required = false) Integer pageSize,
            @PathVariable(value = "currentPage",required = false) Integer currentPage,
            Integer collegeId,
            String majorName
    ) {
        if(pageSize == null || pageSize <= 0){
            pageSize = 10;
        }
        if(currentPage == null || currentPage <= 0){
            currentPage = 1;
        }

        if( collegeId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }


        return majorsService.getMajorPage(pageSize,currentPage,collegeId,majorName);
    }

    /**
     * 新增专业
     * @param majorPo 专业信息
     * @return 反馈
     */
    @PostMapping("/add")
    public Result<String> addMajor(@RequestBody(required = false)MajorPo majorPo){
        if(majorPo == null && majorPo.getCollegeId() == null && majorPo.getName().isEmpty()){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return majorsService.addMajor(majorPo);
    }

    /**
     * 获取专业列表
     * @param collegeId 学院id
     * @return 数据
     */
    @GetMapping("/list/{collegeId}")
    public Result<List<MajorVo>> getMajorList(@PathVariable(value = "collegeId",required = false) Integer collegeId){
        if(collegeId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return majorsService.getMajorList(collegeId);
    }

    /**
     * 获取专业列表（包含二级班级）
     * @param collegeId 学院id
     * @return 数据
     */
    @GetMapping("/major_with_class_list/{collegeId}")
    public Result<List<MajorWithClass>> getMajorWithClassList(@PathVariable(required = false,value = "collegeId") Integer collegeId){
        if(collegeId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return majorsService.getMajorWithClassList(collegeId);
    }
}
